package com.patMall.service.impl;

import com.patMall.entity.Commodity;
import com.patMall.entity.CommodityImg;
import com.patMall.entity.Store;
import com.patMall.entity.User;
import com.patMall.mapper.CommodityImgMapper;
import com.patMall.mapper.CommodityMapper;
import com.patMall.mapper.StoreMapper;
import com.patMall.mapper.UserMapper;
import com.patMall.service.StoreService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author 750909637@qq.com
 * @PackageName patMall
 * @Package com.patMall.service.impl
 * @Date 2024/1/23 16:33
 * @Version 1.0
 */
@Service
public class StoreServiceImpl implements StoreService {
    @Resource
    private StoreMapper storeMapper;
    @Resource
    private UserMapper userMapper;
    @Resource
    private CommodityMapper commodityMapper;
    @Resource
    private CommodityImgMapper commodityImgMapper;
    @Override
    public int updateStore(Store store) {
        return storeMapper.updateStore(store);
    }

    @Override
    public Store getStoreId(Integer storeId) {
        Store store = storeMapper.selectByStoreId(storeId);
        return store;
    }

    @Override
    public List<Store> findAllStore() {
        List<Store> storeList = storeMapper.selectAllStore();
        for (Store store : storeList) {
            User user = userMapper.selectStoreUser(store.getStoreId());
            store.setUser(user);
        }
        return storeList;
    }

    @Override
    public List<Store> findStoreUsername(String storeName) {
        return storeMapper.selectStoreByName(storeName);

    }

    @Override
    public List<Store> findByStoreRandom() {
        List<Store> storeList = storeMapper.selectByStoreRandom();
        for (Store store : storeList){
            List<Commodity> commodityList = commodityMapper.selectByStoreId(store.getStoreId());
            for (Commodity commodity : commodityList){
                List<CommodityImg> commodityImg = commodityImgMapper.getAllByCommodity_id(commodity.getCommodityId());
                commodity.setCommodityImgList(commodityImg);
            }
            store.setCommodityList(commodityList);
        }
        System.err.println(storeList);
        return storeList;
    }

}
